<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="chart" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="showAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体部分包裹了一个修改过的行列布局 -->
        <el-tree default-expand-all :data="list">
          <template v-slot="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <svg-icon icon-class="people" /><span> {{ data.name }} </span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="showAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>

        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      width="50%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <add-or-edit v-if="showDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @close="hCancel" @success="hSuccess" />
    </el-dialog>

  </div>
</template>

<script>
import { delDepartments, getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addOrEdit from './departDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      // list: [{
      //   name: '财务部',
      //   manager: '刘备',
      //   children: [
      //     {
      //       name: '财务核算部',
      //       manager: '张飞'
      //     },
      //     {
      //       name: '税务核算部',
      //       manager: '关羽'
      //     }
      //   ]
      // }],
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    // 创建生命周期完成后 调用发送请求的方法
    this.loadDepartments()
  },
  methods: {
    // 自定义一个方法用来发送请求
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res.data.depts)
        this.originList = res.data.depts.map(({ code, name, pid, id }) => ({ code, name, pid, id }))
        // console.log(this.originList)
        res.data.depts.shift()
        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    showAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    doDel(id) {
      // console.log(id)

      this.$confirm('你确定要删除此部门吗？嘿嘿嘿', '提示', {
        type: 'warning'
      })
        .then(async() => {
          const res = await delDepartments(id)
          // console.log(res)
          this.$message.success(res.message)
          this.hSuccess()
        })
        .catch((e) => {
          console.log(e)
          // this.$message.error(e.message)
        })
    },
    hDel(id) {
      this.doDel(id)
    },
    hCancel() {
      this.showDialog = false
    }
  }
}
</script>
